#[cfg(test)]
mod thread_test{
    use crate::taskpool::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use prost::alloc::sync::Arc;
    use crate::taskpool::lua_pool::{LoalLuaEval, LuaPool};
    use mlua::{Function, Lua};
    use tokio::macros::support::Future;
    use std::pin::Pin;

    #[test]
    fn test_thread_pool(){
        let tp:ThreadPool = ThreadPool::build()
            .set_task(|pool:&ThreadPool|{
                let res:Result<&'static str,_> = pool.channel().recv();
                match res {
                    Ok(s)=> wd_log::log_debug_ln!("成功收到消息：{}",s),
                    Err(e)=> wd_log::log_error_ln!("接受消息失败：{}",e)
                }
            })
            .cset_count(3);
        tp.async_run();
        if let Err(e) = tp.channel().send("hello world"){
            wd_log::log_error_ln!("send error:{}",e)
        }else{
            wd_log::log_debug_ln!("send success")
        }
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
    struct AsyncHandle{
        pub index : Arc<AtomicUsize>
    }
    impl AsyncHandle{
        async fn call(&self,_tp: ThreadPool) {
            let index = self.index.fetch_add(1,Ordering::Relaxed);
            let lua = mlua::Lua::new();
            lua.load(r#"
            function main(index)
                print("异步执行了一次lua",index)
            end
            main(...)
            "#).call_async::<_,()>(index).await.unwrap();
        }
    }
    #[test]
    fn async_test_thread_pool(){
        let index =Arc::new(AtomicUsize::new(0));
        let ah:&'static AsyncHandle = Box::leak(Box::new(AsyncHandle{index}));
        let tp:ThreadPool = ThreadPool::build()
            .set_task(move |tp:&ThreadPool|{
                let tp = tp.clone();
                thread_async_handle!(ah.call(tp).await);
                wd_log::log_debug_ln!("线程结束");
            })
            .cset_count(3);
        tp.async_run();
        std::thread::sleep(std::time::Duration::from_secs(10));
    }
    struct LoadLua;
    impl LoalLuaEval<usize,usize> for LoadLua{
        fn load(&self) -> Pin<Box<dyn Future<Output=(&'static Lua,Function<'static>)>>> {
            let fut = async {
                let lua = Lua::new().into_static();
                let eval = lua.load(r#"
                function main(index)
                    print("异步执行了一次lua",index)
                    return index * 2
                end
                return main
                "#).eval_async::<Function<'static>>().await.unwrap();
                (lua,eval)
            };
            Box::pin(fut)
        }

        fn handle(&self,_lua:&'static Lua,f:Function<'static>,req: usize)->Pin<Box<dyn Future<Output=usize>>> {
            let fut = async move{
                if req == 2 {
                    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
                }
                    f.call_async::<_,usize>(req).await.unwrap()
            };
            Box::pin(fut)
        }
    }

    #[test]
    fn async_test_lua_pool(){
        tokio::runtime::Runtime::new().unwrap()
            .block_on(async{
                let lp =LuaPool::new(LoadLua);
                let tp:ThreadPool = ThreadPool::build()
                    .set_task(lp.clone())
                    .cset_count(3);
                tp.async_run();
                // lp.spawn(1usize).await.unwrap();
                // lp.spawn(2usize).await.unwrap();
                // lp.spawn(3usize).await.unwrap();
                let resp = lp.block(1usize).await.unwrap();
                wd_log::log_debug_ln!("1 ---> 同步收到信息：{}",resp);
                let resp = lp.block(2usize).await.unwrap();
                wd_log::log_debug_ln!("1 ---> 同步收到信息：{}",resp);
                let resp = lp.block(3usize).await.unwrap();
                wd_log::log_debug_ln!("1 ---> 同步收到信息：{}",resp);
                tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
            });
    }


}

